package com.hcbd.tdwt.ui.xingyu.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import com.hcbd.tdwt.base.bd.BdToolFactory;
import com.hcbd.tdwt.MainApp;
import com.hcbd.tdwt.base.BeidouBoxParams;
import com.hcbd.tdwt.base.Constant;
import com.hcbd.tdwt.base.ConvertToBdMsgTool;
import com.hcbd.tdwt.base.GlobalParams;
import com.hcbd.tdwt.ui.xingyu.vo.LocationTimer;
import com.hcbd.tdwt.util.SharedPreferencesUtil;
import com.hcbd.tdwt.ui.xingyu.vo.PositionVo;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class LocationReportService extends Service {
    private Timer timer;
    private SharedPreferencesUtil sp;

    private LocationTimer locationTimer;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        locationTimer=new LocationTimer();
        sp=new SharedPreferencesUtil(MainApp.getInstance());
        //delay毫秒秒后开始
        final long delay = GlobalParams.locationReportFrequency+2;
        //long period = GlobalParams.locationReportFrequency * 1000+2000;
        //判断终端是否正常连接
        if (!BeidouBoxParams.isBoxConnectNormal) {
            return super.onStartCommand(intent, flags, startId);
        }
        TimerTask task = new TimerTask() {
            int index=0;
            @Override
            public void run() {
                if (GlobalParams.locationReportFrequency == 0) {
                    return;
                }
                if(!BeidouBoxParams.isBoxConnectNormal) {
                    MainApp.getInstance().showMsg("发送失败,串口已关闭!");
                    return;
                }
                if(index==0){
                    List<PositionVo>list=convertPosition();
                    if(list!=null&&list.size()>0){
                        bdReportLocation(list);
                    }else{
                        MainApp.getInstance().showMsg("当前未获取到定位信息,发送失败!");
                    }
                    GlobalParams.positions.clear();
                    index=(int)delay;
                }else{
                    index--;
                    locationTimer.time=index;
                }
                EventBus.getDefault().post(locationTimer);
            }
        };
        //从现在起过delay毫秒以后，每隔period毫秒执行一次。
        if (timer == null) {
            timer = new Timer();
        }
        timer.schedule(task, delay, 1000);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("onDestroy","服务关闭！");
        if(timer!=null) {
            timer.cancel();
        }
        locationTimer.time=0;
        EventBus.getDefault().post(locationTimer);
        timer = null;
    }
    /**
     * 北斗网上报位置
     */
    private void bdReportLocation(List<PositionVo> positions) {
        String bdContent;
        int uploadPrpType=sp.getAttributeInt(Constant.UPLOAD_PRO);
        if(uploadPrpType==2){
            BdToolFactory.getBdSendBase().sendEPQInfo(GlobalParams.rdCentreNumber, 2,0,0);
            MainApp.getInstance().startNewSentWaitSecTimer();
            return;
        }else if(uploadPrpType==-1){
            bdContent=ConvertToBdMsgTool.castUserMessageTo0x04N5(positions, (int)GlobalParams.locationReportFrequency);
        }else{
            bdContent=ConvertToBdMsgTool.castUserMessageTo0x15(positions, MainApp.getInstance().getLocationSerial());
        }
        BdToolFactory.getBdSendBase().sendMsg(GlobalParams.rdCentreNumber,2,2,bdContent);
        // 发送状态倒计时线程
        MainApp.getInstance().startNewSentWaitSecTimer();
    }

    public List<PositionVo> convertPosition(){
        int size=GlobalParams.positions.size();
        if(size==0){
            return null;
        }
        List<PositionVo> positions = new ArrayList<>();
        if(GlobalParams.locationReportFrequency<=60){//一分钟
            if(5>size){
                positions=GlobalParams.positions;
            }else{
                positions=GlobalParams.positions.subList(size-4,size);
            }
        }else if(GlobalParams.locationReportFrequency<=120){//二分钟
            positions=getPosition(size,2);
        }else if(GlobalParams.locationReportFrequency<=180){//三分钟
            positions=getPosition(size,3);
        }else if(GlobalParams.locationReportFrequency<=300){//五分钟
            positions=getPosition(size,5);
        }else if(GlobalParams.locationReportFrequency<=600){//10分钟
            positions=getPosition(size,10);
        }else if(GlobalParams.locationReportFrequency<=1800){//30分钟
            positions=getPosition(size,30);
        }else if(GlobalParams.locationReportFrequency<=3600){//60分钟
            positions=getPosition(size,60);
        }else if(GlobalParams.locationReportFrequency<=7200){//120分钟
            positions=getPosition(size,120);
        }
        return  positions;
    };
    public  List<PositionVo> getPosition(int size, int index){
        List<PositionVo> positions = new ArrayList<>();
        if(5>size){
            return GlobalParams.positions;
        }else if(size>=(index*4)){
            positions.add(GlobalParams.positions.get(0));
            positions.add(GlobalParams.positions.get(index));
            positions.add(GlobalParams.positions.get(index*2));
            positions.add(GlobalParams.positions.get(index*3));
            //positions.add(GlobalParams.positions.get(index*4));
        }else{
            for(int i=0;(i*index)<size;i++){
                positions.add(GlobalParams.positions.get(i*index));
            }
        }
        return positions;
    }
}

